package com.beeantec.jnrerc.modules.course.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beeantec.jnrerc.core.api.dto.PagingReqDTO;


import com.beeantec.jnrerc.core.utils.PageUtil;


import com.beeantec.jnrerc.dto.CourseChapterDTO;
import com.beeantec.jnrerc.dto.CourseDTO;
import com.beeantec.jnrerc.dto.CourseIntroduceDTO;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.response.CourseChapterPeriodDTO;
import com.beeantec.jnrerc.dto.response.CourseInfoDTO;
import com.beeantec.jnrerc.entity.*;
import com.beeantec.jnrerc.mapper.CourseMapper;
import com.beeantec.jnrerc.modules.course.service.*;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    CourseIntroduceService courseIntroduceService;

    @Autowired
    CourseChapterService courseChapterService;

    @Autowired
    CourseChapterPeriodService courseChapterPeriodService;

    @Autowired
    CourseMemberStudyLogService courseMemberStudyLogService;

    @Autowired
    MemberService memberService;
    @Override
    public IPage<CourseDTO> paging(PagingReqDTO<CourseDTO> reqDTO) {

        //创建分页对象
        Page page = new Page(reqDTO.getCurrent(), reqDTO.getSize());

        //转换结果
        IPage<CourseDTO> pageData = baseMapper.paging(page, reqDTO.getParams());
        return pageData;
    }

    /**
     * 课程详情接口
     *
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoDTO info(String token,Long courseId) {
        // 课程信息
        Course course = baseMapper.selectById(courseId);
        if (course == null) {
            return null;
        }
        CourseInfoDTO data = new CourseInfoDTO();
        BeanUtil.copyProperties(course, data);

        // 课程介绍
        CourseIntroduce courseIntroduce = courseIntroduceService.getById(data.getIntroduceId());
        if (!StringUtils.isEmpty(courseIntroduce)) {
            data.setIntroduce(BeanUtil.copyProperties(courseIntroduce, CourseIntroduceDTO.class).getIntroduce());
        }


        // 章节信息
        QueryWrapper<CourseChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.lambda().eq(CourseChapter::getCourseId,courseId);
        List<CourseChapter> courseChapterList = courseChapterService.list(chapterQueryWrapper);
        if (CollectionUtil.isNotEmpty(courseChapterList)) {
            data.setChapterList(PageUtil.copyList(courseChapterList, CourseChapterDTO.class));
        }

        // 课时信息
        if (CollectionUtil.isNotEmpty(data.getChapterList())) {
            for (CourseChapterDTO courseChapterDTO : data.getChapterList()) {
                QueryWrapper<CourseChapterPeriod> chapterPeriodQueryWrapper = new QueryWrapper<>();
                chapterPeriodQueryWrapper.lambda().eq(CourseChapterPeriod::getCourseId,courseId)
                .eq(CourseChapterPeriod::getChapterId,courseChapterDTO.getId());
                List<CourseChapterPeriod> courseChapterPeriodList = courseChapterPeriodService.list(chapterPeriodQueryWrapper);
                List<CourseChapterPeriodDTO> courseChapterPeriodDTOS = new ArrayList<>();
                for (CourseChapterPeriod courseChapterPeriod:courseChapterPeriodList){
                    courseChapterPeriodDTOS.add(getCourseChapterPeriod(token,courseChapterPeriod));
                }
                courseChapterDTO.setPeriodList(courseChapterPeriodDTOS);
            }
        }
        return data;
    }


    private CourseChapterPeriodDTO getCourseChapterPeriod(String token, CourseChapterPeriod period){
        CourseChapterPeriodDTO courseChapterPeriodDTO = new CourseChapterPeriodDTO();
        BeanUtils.copyProperties(period,courseChapterPeriodDTO);
        MemberVo memberVo = memberService.token(token);
        QueryWrapper<CourseMemberStudyLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CourseMemberStudyLog::getCourseId,period.getCourseId())
                .eq(CourseMemberStudyLog::getPeriodId,period.getId())
                .eq(CourseMemberStudyLog::getMemberId,memberVo.getMemberId());
        CourseMemberStudyLog memberStudyLog = courseMemberStudyLogService.getOne(queryWrapper);
        if (null==memberStudyLog){
            courseChapterPeriodDTO.setPeriodState(0);
        }else {
            courseChapterPeriodDTO.setPeriodState(memberStudyLog.getPeriodState());
        }
        return courseChapterPeriodDTO;
    }
}
